package com.ybl.store.backend.controller.item;

import com.ybl.store.backend.dao.entity.item.VoucherDO;
import com.ybl.store.backend.dto.req.item.voucher.VoucherSaveReqDTO;
import com.ybl.store.backend.dto.req.item.voucher.VoucherUpdateReqDTO;
import com.ybl.store.backend.dto.resp.item.voucher.VoucherNameRespDTO;
import com.ybl.store.backend.service.item.VoucherService;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 卡券控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/voucher")
public class VoucherController {

    private final VoucherService voucherService;

    /**
     * 新增卡券
     */
    @PostMapping
    public Result<Void> saveVoucher(@RequestBody VoucherSaveReqDTO requestParam) {
        voucherService.saveVoucher(requestParam);
        return Results.success();
    }

    /**
     * 删除卡券
     */
    @DeleteMapping
    public Result<Void> deleteVoucher(@RequestParam Long id) {
        voucherService.deleteVoucher(id);
        return Results.success();
    }

    /**
     * 修改卡券
     */
    @PutMapping
    public Result<Void> updateVoucher(@RequestBody VoucherUpdateReqDTO requestParam) {
        voucherService.updateVoucher(requestParam);
        return Results.success();
    }

    /**
     * 禁用卡券
     */
    @PutMapping("/ban")
    public Result<Void> banVoucher(@RequestParam Long id, @RequestParam Integer banFlag) {
        voucherService.banVoucher(id, banFlag);
        return Results.success();
    }

    /**
     * 分页查询卡券
     */
    @GetMapping("/page")
    public Result<ListRespDTO<VoucherDO>> pageVoucher(
            @RequestParam("pn") Integer pn,
            @RequestParam("ps") Integer ps,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String serial)
    {
        return Results.success(voucherService.pageVoucher(pn, ps, name, serial));
    }

    /**
     * 查询卡券列表
     */
    @GetMapping("/list/name")
    public Result<ListRespDTO<VoucherNameRespDTO>> listVoucherName () {
        return Results.success(voucherService.listVoucherName());
    }
}
